/**
 * 商店界面
 */
class mallSub extends eui.Component{
    private return:eui.Button;
    private fleshy:eui.Image;
    private flower:eui.Image;
    private pet:eui.Image;
    private buy:eui.Button;
    private note:eui.Label;
    
    private mallScroll:eui.Scroller;
    private mallList:eui.List;

    private petScroll:eui.Scroller;
    private petList:eui.List;

    private _arrayData:eui.ArrayCollection;

    private _fleshyData:Array<any> = [];
    private _flowerData:Array<any> = [];
    private _petData:Array<any> = [];
    private _page:string = "fleshy";//多肉：fleshy,鲜花:flower,宠物:pet

    private _payListUI:payList;

    private _petId:number = 0;

    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "mallSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.inIt();
    }

    private async inIt(){
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.dispatchEventWith("return");
        },this);
        this.fleshy.addEventListener(egret.TouchEvent.TOUCH_TAP,this.changeGoods,this);
        this.flower.addEventListener(egret.TouchEvent.TOUCH_TAP,this.changeGoods,this);
        this.pet.addEventListener(egret.TouchEvent.TOUCH_TAP,this.changeGoods,this);
        this.buy.addEventListener(egret.TouchEvent.TOUCH_TAP,this.changeGoods,this);

        Dispatcher.ins.addEventListener("shopList",this.assignValue,this);
        Dispatcher.ins.addEventListener("paySuccess",this.paySuccess,this);
        Dispatcher.ins.addEventListener("createOrder",this.createOrder,this);

        this._arrayData =  new eui.ArrayCollection(this._fleshyData);
        this.mallList.dataProvider = this._arrayData;
        this.mallList.itemRenderer = mallFlIR;
        this.getList();

        this.petList.itemRenderer = mallPetIR;
        this.petList.addEventListener("petId",(evt:egret.Event)=>{
            this._petId = evt.data;
        },this);
    }
    private changeGoods(evt:egret.TouchEvent):void{
        let btn = evt.currentTarget;
        switch (btn){
            case this.fleshy:{
                 this.note.text = "所有多肉5份包邮,可混搭";
                 this._page = "fleshy";
                 this.fleshy.source = "icon_fleshy_on_png";
                 this.flower.source = "icon_flower_off_png";
                 this.pet.source = "icon_pet_off_png";
                 if(this._fleshyData.length == 0){
                     this.getList();
                 }
                 this._arrayData =  new eui.ArrayCollection(this._fleshyData);
                 this.mallList.dataProvider = this._arrayData;
                 this._arrayData.refresh();
                 this.mallScroll.visible = true;
                 this.petScroll.visible = false;
                 this.buy.visible = true;
                 break;
            }
            case this.flower:{
                this.note.text = "所有鲜花12份包邮，不可混搭";
                this._page = "flower";
                this.fleshy.source = "icon_fleshy_off_png";
                this.flower.source = "icon_flower_on_png";
                this.pet.source = "icon_pet_off_png";
                if(this._flowerData.length == 0){
                     this.getList();
                 }
                this._arrayData =  new eui.ArrayCollection(this._flowerData);
                this.mallList.dataProvider = this._arrayData;
                this._arrayData.refresh();
                this.mallScroll.visible = true;
                this.petScroll.visible = false;
                this.buy.visible = true;
                break;
            }
            case this.pet:{
                this.note.text = "";
                this._page = "pet";
                this.fleshy.source = "icon_fleshy_off_png";
                this.flower.source = "icon_flower_off_png";
                this.pet.source = "icon_pet_on_png";
                this.mallScroll.visible = false;
                this.petScroll.visible = true;
                this.buy.visible = false;
                if(this._petData.length == 0){
                     this.getList();
                }
                this._arrayData =  new eui.ArrayCollection(this._petData);
                this.petList.dataProvider = this._arrayData;
                this._arrayData.refresh();
                break;
            }
            case this.buy:{
                let arr = [];
                let trArr = [];
                if(this._page == "fleshy"){
                    arr = this._fleshyData;
                }else if(this._page == "flower"){
                    arr = this._flowerData;
                }
                for(let i = 0, lg = arr.length;i < lg;i ++){
                    let obj = arr[i];
                    if(obj.num.substr(0,1) == "X"){
                        obj.num = obj.num.substr(1);
                    }
                    if(obj.num > 0){
                        obj.num = "X" + obj.num;
                        trArr.push(obj);
                    }
                }
                if(trArr.length == 0){
                    Dispatcher.ins.dispatchEvent(new GameEvent("toast","请先选择要购买的商品"));
                    return;
                }
                if(!this._payListUI){
                    this._payListUI = new payList(trArr);
                }else{
                    this._payListUI.valueList(trArr);
                }
                this.addChild(this._payListUI);
                break;
            }
        }
    }
    public getList():void{
        let dataPost:any = {};
        let type:number = 0;
        if(this._page == "fleshy"){
            type = 1;
        }else  if(this._page == "flower"){
            type = 0;
        }else{
            type = 2;
        }
        dataPost.sType = type;
        socket.request("shopList",dataPost,type);
    }
    public assignValue(evt:GameEvent):void{
        let obj = evt.data;
        if(obj.note == 3) return;
        let list = obj.list;
        let arr = [];
        for(let i = 0,lg = list.length;i < lg;i ++){
            let trObj:any = list[i];
            if(i < 3){
                trObj.recommend = true;
            }else{
                trObj.recommend = false;
            }
            trObj.icon = "sh_"+ list[i].Id+"_png";
            trObj.num = "0";
            trObj.Price = "￥" + trObj.Price;
            trObj.ParPrice = "￥" + trObj.ParPrice;
            arr.push(trObj);
        }
        switch (obj.note){
            case 0:{
                this._flowerData = arr;
                this._arrayData = new eui.ArrayCollection(arr);
                this.mallList.dataProvider = this._arrayData;
                this._arrayData.refresh();
                break;
            }
            case 1:{
                this._fleshyData = arr;
                this._arrayData = new eui.ArrayCollection(arr);
                this.mallList.dataProvider = this._arrayData;
                this._arrayData.refresh();
                break;
            }
            case 2:{
                this._petData = arr;
                this._arrayData = new eui.ArrayCollection(arr);
                this.petList.dataProvider = this._arrayData;
                this._arrayData.refresh();
                break;
            }
        }
        
    }
    private paySuccess():void{
        if(this._page !== "pet"){
            this.removeChild(this._payListUI);
            this.getList();
        }else{
            Dispatcher.ins.dispatchEvent(new GameEvent("createPet",this._petId));
        }
        this.dispatchEventWith("return");
    }
    private createOrder(evt:GameEvent):void{
        let obj = evt.data;
        if(obj.note == "pet"){
            this.wxPay(obj.data);
        }
    }
    private wxPay(data):void{
        if(wx) {
            wx.chooseWXPay({
                timestamp: data.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                nonceStr: data.nonceStr, // 支付签名随机串，不长于 32 位
                package: data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                signType: data.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                paySign: data.sign, // 支付签名
                success: function (res) {
                    // 支付成功后的回调函数
                    if(res.errMsg == "chooseWXPay:ok"){
                        Dispatcher.ins.dispatchEvent(new GameEvent("paySuccess"));   
                    }
                }
            });
        }
    }
}
class mallFlIR extends eui.ItemRenderer{
    private recommend:eui.Image;
    private sign:eui.Rect;
    private add:eui.Rect;
    private num:eui.Label;
    private _num:number = 0;
    private addYY:eui.Image;
    private signYY:eui.Image;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "mallFlIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.sign.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            if(this._num - 1 >= 0){
                this._num = this._num - 1;
            }
            this.num.text = this._num + ""; 
            this.dataChanged();
        },this);
        this.sign.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
            if(this._num == 0) return;
            this.signYY.visible = true;
        },this);
        this.sign.addEventListener(egret.TouchEvent.TOUCH_END,()=>{
            this.signYY.visible = false;
        },this);
        this.sign.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{
            this.signYY.visible = false;
        },this);
        this.add.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this._num = this._num + 1;
            this.num.text = this._num + ""; 
            this.dataChanged();
        },this);
        this.add.addEventListener(egret.TouchEvent.TOUCH_BEGIN,()=>{
            this.addYY.visible = true;
        },this);
        this.add.addEventListener(egret.TouchEvent.TOUCH_END,()=>{
            this.addYY.visible = false;
        },this);
        this.add.addEventListener(egret.TouchEvent.TOUCH_MOVE,()=>{
            this.addYY.visible = false;
        },this);
    }
    protected dataChanged():void{
        this.data.num = this._num + "";
        if(this.data.recommend){
            this.recommend.visible = true;
        }else{
            this.recommend.visible = false;
        }
    }
}
class mallPetIR extends eui.ItemRenderer{
    private buy:eui.Button;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "mallPetIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.buy.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            let obj = this.data;
            let orderList:any = [];
           
            let trObj:any = {};
            trObj.propId = obj.Id;
            trObj.propName = obj.PropName;
            trObj.propNum = 1;
            trObj.price = obj.Price.substr(1);
            trObj.parPrice = obj.ParPrice.substr(1);
            trObj.sType = obj.SType;
            orderList.push(trObj);
            let dataPost = {
                "userId":common.userInfo.userId,
                "areaId":common.areaId,
                "gameId":common.gameId,
                "chnltype":common.chnltype,
                "numId":common.userInfo.numId,
                "orderList":orderList,
                "version":common.version
            }
            this.parent.dispatchEventWith("petId",true,obj.Id);
            http.getHttpServer("createOrder",dataPost,"pet");
        },this);
    }
    protected dataChanged():void{

    }
}